# Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
# SPDX-License-Identifier: Apache-2.0

# IoT-SDK

cmake_minimum_required(VERSION 3.21.0 FATAL_ERROR)

project(open-iot-sdk LANGUAGES C CXX ASM)

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/cmake)
if (NOT CMAKE_CXX_STANDARD)
    set(CMAKE_CXX_STANDARD 14)
endif()
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_C_STANDARD 99)
set(FETCHCONTENT_QUIET OFF)

set(IOTSDK_FETCH_LIST "" CACHE STRING
    "List of Open IoT SDK components to fetch. See cmake/DeclareSdkComponents.cmake for available components.")

# This allows us to have components declared before adding some tests that are
# added before components.
include(DeclareSdkComponents)

if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
    include(CTest)
    include(AddTestTools)
endif()

# CMSIS-Pack-Utils tool is an essential part of our build system so fetch it unconditionally.
FetchContent_MakeAvailable(cmsis-pack-utils)
include("${cmsis-pack-utils_SOURCE_DIR}/modules/CMSIS-Pack-Utils.cmake")

if("trusted-firmware-m" IN_LIST IOTSDK_FETCH_LIST)
    set(TFM_CMAKE_ARGS "" CACHE STRING "Additional CMake arguments passed to TF-M (semicolon-separated)")
    set(TFM_COMMAND "" CACHE STRING "Command to add directives when building TF-M")
    set(TFM_PATCH_COMMAND "" CACHE STRING "Command to patch TF-M by applying a patch file")
endif()

if("ml-embedded-evaluation-kit" IN_LIST IOTSDK_FETCH_LIST)
    set(ML_CMAKE_ARGS "" CACHE STRING "Additional CMake arguments passed to ml-embedded-evaluation-kit (semicolon-separated)")
    set(ML_PATCH_COMMAND "" CACHE STRING "Command to patch ML Embedded Evaluation Kit")
    set(ML_TARGETS "all" CACHE STRING "Targets to build from the ML Embedded Evaluation Kit")
endif()

list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_LIST_DIR}/cmake)

if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
    # Enable features of project_options.
    # Note: We need to enable project_options after adding fetched components
    # and before adding local components, to ensure options are only applied to
    # local components.
    FetchContent_Declare(
        project_options
        GIT_REPOSITORY  https://github.com/cpp-best-practices/project_options.git
        GIT_TAG         v0.33.0
    )
    FetchContent_MakeAvailable(project_options)
    include(${project_options_SOURCE_DIR}/Index.cmake)
    include(${project_options_SOURCE_DIR}/src/DynamicProjectOptions.cmake)

    # cppcheck will be run by pre-commit checks
    set(ENABLE_CPPCHECK_DEVELOPER_DEFAULT OFF)

    include(AddToolchainWarningFlags)
    set(GCC_WARNINGS ${IOTSDK_WARNING_FLAGS} ${IOTSDK_WARNING_AS_ERRORS_FLAGS})
    set(CLANG_WARNINGS ${GCC_WARNINGS})

    dynamic_project_options(
        CLANG_WARNINGS
        ${CLANG_WARNINGS}
        GCC_WARNINGS
        ${GCC_WARNINGS}
    )
    if (OPT_ENABLE_COVERAGE AND NOT CMAKE_CROSSCOMPILING)
        include(GenerateCodeCoverageReport)
    endif()
endif()


# `utils` depends on some targets created by the `components`. Therefore `components` must be added
# before `utils`.
if(CMAKE_CROSSCOMPILING)
    add_subdirectory(components EXCLUDE_FROM_ALL)
else()
    add_subdirectory(components)
endif()

add_subdirectory(utils)
